package org.hgvc.webapp.ets.jsf.beans.admin;

import org.hgvc.webapp.ets.bo.Partner;
import org.hgvc.webapp.ets.bo.PartnerProduct;
import org.hgvc.webapp.ets.spring.service.PartnerService;
import org.jocsb.base.AbstractBean;
import org.jocsb.base.exception.BaseException;
import org.jocsb.base.util.SpringUtils;


public class AdminPartnerProductApprovalBean extends AbstractBean {

	public static String BEAN_NAME="adminPartnerProductApprovalBean";
	public static String AB_INVOKE_ACTION="partnerProductApprovalInvokeAction";
	private PartnerService service;
	private Partner partner;
	private String reason;
	private String code;
	private String actionNo;
	private PartnerProduct product;
	public static final String NAV_PRODUCT_APPROVAL="adminPartnerProductAppr";

	public Partner getPartner() {
		return partner;
	}
	public void setPartner(Partner partner) {
		this.partner = partner;
	}
	public String getReason() {
		return reason;
	}
	public void setReason(String reason) {
		this.reason = reason;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public PartnerProduct getProduct() {
		return product;
	}
	public void setProduct(PartnerProduct partnerProduct) {
		this.product = partnerProduct;
	}
	public void setService(PartnerService service) {
		this.service = service;
	}
	/*-------------------------------------------------------------------------*/
	public String partnerProductApprovalInvokeAction(String actionNo,String... params){
		try{
			code = null;
			reason = null;
			this.actionNo = actionNo;
			product = service.getPartnerProductById(params[1]);
			partner = product.getPartner();
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return NAV_PRODUCT_APPROVAL.concat("_"+getRoleName());
	}

	public String approvedAction(){
		try{
			if(code==null||code.length()==0){
				throw new BaseException(SpringUtils.getMessage("error.product.code.required", new Object[]{}));
			}
			service.approvePartnerProduct(partner, product, getThisUser(), code, reason, actionNo);

			return navPortal();
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return NAV_PRODUCT_APPROVAL.concat("_"+getRoleName());
	}

	public String rejectAction(){
		try{
			
			service.rejectPartnerProduct(partner, product, getThisUser(), reason, actionNo);
			return navPortal();
		}catch(BaseException be){
			be.printStackTrace();
			initErrorBean(be);
		}
		return NAV_PRODUCT_APPROVAL.concat("_"+getRoleName());
	}

	public String navPortal(){
		return AdminPortalBean.NAV_ADMIN_PORTAL.concat("_"+getRoleName());
	}
}
